package com.example.myapplication8;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AddBookActivity extends AppCompatActivity {
    private MyDatabaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);
        helper = new MyDatabaseHelper(this,"BookLibrary.db",null,2);
        Button addBookButton = findViewById(R.id.add_book_button);
        TextView addBookName = findViewById(R.id.add_bookname);
        TextView addBookPrice = findViewById(R.id.add_bookprice);
        TextView addBookType = findViewById(R.id.add_booktype);
        addBookButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = helper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("name",addBookName.getText().toString());
                contentValues.put("price",addBookPrice.getText().toString());
                contentValues.put("category_id",addBookType.getText().toString());
                db.insert("Book",null,contentValues);
                Log.d("AddBookActivity", "onClick: 添加成功");
                Intent intent = new Intent(AddBookActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

    }
}